



var M_id = '1';  // 默认组织ID
var M_pid = '0';  // 默认组织父ID
var M_isParent; // 默认是否为节点
var columnTree;
var orgIdStrs;//已分配的组织字符串

//加载树形菜单
$(document).ready(function(){
	//alert("sss1:"+orgIdStrs);
	orgIdStrs = $('#orgIdStrs').val();
	var setting = {
			async : {
				enable : true,
				url :contextPath+'ac/user/list_tree',
				type : 'post',
				autoParam : [ 'id'],
				otherParam :{orgIdStrs:orgIdStrs,flag:'edit'}
			},
			
			check: {
				enable: true
			},
			callback : {
				onCheck: onCheck,
				onAsyncError:uppZtreeAjaxError
			}
			
	};
	columnTree = $.fn.zTree.init($('#treeList'), setting);
	setCheck();
});


function setCheck() {
	var zTree = $.fn.zTree.getZTreeObj("treeList"),
	type = { "Y":"", "N":""};// 不级联勾选
	zTree.setting.check.chkboxType = type;
}

function setNodeCheckAndOPen(){
	var zTree = $.fn.zTree.getZTreeObj("treeList"),
	nodes = zTree.getNodes();
	alert("nodes.length:"+nodes.length);
	var listUserOrgs = $('#listUserOrgs').val();
	for (var i=0; i < nodes.length; i++) {
		for( var j = 0; j < listUserOrgs.length; j++){
			if(listUserOrgs[j].orgId ==  nodes[i].id){
				nodes[i].checked = true;
				nodes[i].open = true;
			}
		}
	}
}

/**
 * 勾选组织，将id收集起来
 */
function onCheck(event, treeId, treeNode){
	var checkedOrgIds = '';
	var zTree = $.fn.zTree.getZTreeObj("treeList"),
	nodes = zTree.getCheckedNodes(true);
	
	if(nodes.length>0){
		for (var i=0; i < nodes.length; i++) {
			checkedOrgIds += nodes[i].id + ',';
		}
		checkedOrgIds = checkedOrgIds.substring(0, checkedOrgIds.length - 1);
	}
	$('#checkedOrgIds').val(checkedOrgIds);
}


function gotoBack() {
	var _contextPath = $("#contextPath").val();
	window.location.href = _contextPath + "ac/user/toListView";
}


/**
 * 基本信息验证
 */
function validate() {
	
	if ($.trim($("#userName").val()).length<=0){
		$("#userName").val($.trim($("#userName").val()));
		$("#userName").focus();
		return false;
	}else if($.trim($("#userName").val()).length>10){
		top.uppShow('用户名超过了10个字符');
		$("#userName").focus();
		return false;
	}
	
	if ($.trim($("#loginName").val()).length<=0){
		$("#loginName").val($.trim($("#loginName").val()));
		$("#loginName").focus();
		return false;
	}
	
	if(!checkusername($.trim($("#loginName").val()))){
		top.uppShow('登录名在6-12位之间，且由英文大小写、数字或者下划线或者 3 者的组合构成');
		$("#loginName").focus();
		return false;
	}

	if ($.trim($("#phone").val()).length<11){
		$("#phone").val($.trim($("#phone").val()));
		top.uppShow('手机号码必须是11位数字');
		$("#phone").focus();
		return false;
	}
	
	return true;
}

function checkusername(str){ 
	return str.match(/^([u4e00-u9fa5]|[ufe30-uffa0]|[a-za-z0-9_]){6,12}$/);
}

/**
 * 下一步
 */
function goNext() {
	if(!validate()){
		return false;
	}
	$("#create_First").hide();
	$("#create_seconde").show();
}
/**
 * 上一步
 */
function goPreview() {
	$("#create_seconde").hide();
	$("#create_First").show();
}

/**
 * 判断当前列表是否是数组
 */
function isArray(o) {
	if (typeof o == 'object' && typeof o.length == 'number')
		return true;
	else
		return false;
}

/**
 * 检查当前列表的选择个数
 */
function checkedCount(o) {
	if (isArray(o)) {
		var count = 0;
		for ( var i = 0; i < o.length; i++)
			if (o[i].checked)
				count++;
		return count;
	} else {
		if (o.checked)
			return 1;
		else
			return 0;
	}
}

/**
 * 收集勾选项的id
 * 
 * @return
 */

function getCheckBoxIds() {
	var checkid = document.getElementsByName('operatorRoles');
	var id = '';
	if (isArray(checkid)) {
		if (checkedCount(checkid) != 0) {
			for ( var i = 0; i < checkid.length; i++)
				if (checkid[i].checked) {
					id += checkid[i].value + ',';
				}
			id = id.substring(0, id.length - 1);
		}
	} else {
		if (checkid.checked) {
			id += checkid[i].value;
		}
	}
	return id;
}

/**
 * 提交
 * 
 * @return
 */
function submit() {
	
	var selectedRoleIds = getCheckBoxIds();
	var userName = $('#userName').val();
	var loginName = $('#loginName').val();
	var phone = $('#phone').val();
	var email = $('#email').val();
	var remark = $('#remark').val();
	var checkedOrgIds = $('#checkedOrgIds').val();
	var operatorId = $('#operatorId').val();
	var status = $('#status').val();
	
	$.ajax({
		type : 'POST',
		url : contextPath+'ac/user/update',
		data : {operatorId:operatorId,status:status,userName:userName,loginName:loginName,phone:phone,email:email,remark:remark,selectedRoleIds:selectedRoleIds,checkedOrgIds:checkedOrgIds},
		dateType : 'json',
		success : function (root) {
			top.uppShow(root.msgDesc);
			if (root.opResult == '0') {
				top.refreshSpecifiedTab(top.UppTabs.operator);
				top.closeCurrentTab(top.getCurrentTagTitle());
				//window.location.href = contextPath + "ac/user/toListView";//当前页面不刷新
			}
		},
		error : function(data){
		     uppAjaxError(data);
		}
	});
	
}